home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / VideoToolboxSources / ImageStatistics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-27  |  1.8 KB  |  70 lines  |  [TEXT/MMCC]

  1. /*
  2. ImageStatistics.c
  3.  
  4. ImageStatistics returns min, max, mean, and meanSquare values of the pixels in
  5. the supplied rect, after clipping with the GWorld's portRect. The supplied
  6. rect is replaced by the clipped rect.
  7.  
  8. ImageEnergy returns the summed square value, over the clipped rect area, of
  9. the pixels after subtracting the supplied "background".
  10.  
  11. HISTORY:
  12. 10/27/94 wrote it
  13. */
  14. #include "VideoToolbox.h"
  15. #include <assert.h>
  16.  
  17. double ImageEnergy(GWorldPtr aWorld,Rect *aRect,double background)
  18. {
  19.     double mean,meanSquare,area;
  20.  
  21.     ImageStatistics(aWorld,aRect,NULL,NULL,&mean,&meanSquare);
  22.     area=(double)(aRect->right-aRect->left)*(aRect->bottom-aRect->top);
  23.     return area*(meanSquare-2*background*mean+background*background);
  24. }
  25.  
  26. void ImageStatistics(GWorldPtr world,Rect *rect
  27.     ,long *minPtr,long *maxPtr,double *meanPtr,double *meanSquarePtr)
  28. {
  29.     register long pix,pixMax,pixMin,meanL,meanSquareL;
  30.     register double mean,meanSquare;
  31.     double area;
  32.     int width;
  33.     register int i,j;
  34.     int error=0;
  35.     unsigned long pixels[1024];
  36.  
  37.     assert(StackSpace()>4000);
  38.     SectRect(rect,&world->portRect,rect);
  39.     if(EmptyRect(rect)){
  40.         mean=meanSquare=0.0/0.0;    // NAN
  41.         pixMin=pixMax=0;
  42.     }else{
  43.         width=rect->right-rect->left;
  44.         mean=meanSquare=0;
  45.         pixMin=LONG_MAX;
  46.         pixMax=LONG_MIN;
  47.         for(j=rect->top;j<rect->bottom;j++){
  48.             GetWindowPixelsQuickly((WindowPtr)world,rect->left,j,pixels,width);
  49.             meanL=0;
  50.             meanSquareL=0;
  51.             for(i=width-1;i>=0;i--){
  52.                 pix=pixels[i];
  53.                 if(pix<pixMin)pixMin=pix;
  54.                 if(pix>pixMax)pixMax=pix;
  55.                 meanL+=pix;
  56.                 meanSquareL+=pix*pix;
  57.             }
  58.             mean+=meanL;                // float
  59.             meanSquare+=meanSquareL;    // float
  60.         }
  61.         area=(double)width*(rect->bottom-rect->top);
  62.         mean/=area;
  63.         meanSquare/=area;
  64.     }
  65.     if(minPtr!=NULL)*minPtr=pixMin;
  66.     if(maxPtr!=NULL)*maxPtr=pixMax;
  67.     if(meanPtr!=NULL)*meanPtr=mean;
  68.     if(meanSquarePtr!=NULL)*meanSquarePtr=meanSquare;
  69. }
  70.